package com.wn.web;

import com.github.pagehelper.PageInfo;
import com.wn.annotation.RequireRoles;
import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.common.PageParam;
import com.wn.ov.*;
import com.wn.pojo.Cost;
import com.wn.pojo.HospitalizedApplication;
import com.wn.pojo.PatientDetail;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import com.wn.service.CostService;
import com.wn.service.HospitalizedApplicationService;
import com.wn.service.PatientService;
import com.wn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 出院处理业务
 */
@RestController
@RequestMapping("hospital")
public class HospitalizedApplicationController {

    @Autowired
   private  HospitalizedApplicationService has;

    @Autowired
    private CostService costService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private UserService userService;


    /**
     * 添加出院申请
     * @return
     */
    @PostMapping("addHa")
    public ServerResponse addHa(@RequestBody HospitalizedApplication ha){
        ServerResponse serverResponse = has.addHa(ha);
        return serverResponse;
    }

    /**
     * 停止其医嘱
     */
    @PostMapping("updateStatus")
    public ServerResponse updateStatus(Integer id){
        ServerResponse serverResponse = has.updateStatus(id);
        return serverResponse;
    }

    /**
     * 床位状态的修改
     * @param ha
     * @return
     */
    @PostMapping("updateIsUsed")
    public ServerResponse updateIsUsed(@RequestHeader("token") String token,@RequestBody HospitalizedApplication ha){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = has.updateIsUsed(ha,userOv.getId());
        return serverResponse;
    }

    /**
     * 核算费用
     */
    @PostMapping("updateCost")
    public ServerResponse updateCost(@RequestHeader("token") String token,@RequestBody Cost cost){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = costService.updateCost(cost,userOv.getId());
        return serverResponse;
    }

    //查询药品的费用
    @GetMapping("findCostDrugByPatientId")
    public ServerResponse findCostDrugByPatientId(Integer patientId){
        ServerResponse serverResponse = costService.findCostDrugByPatientId(patientId);
        return  serverResponse;
    }

    //查询其他的费用
    @GetMapping("findCostOtherByPatientId")
    public ServerResponse findCostOtherByPatientId(Integer patientId){
        ServerResponse serverResponse = costService.findCostOtherByPatientId(patientId);
        return serverResponse;
    }

    //查询总的费用
    @GetMapping("findCostByPatientId")
    public ServerResponse findCostByPatientId(Integer patientId){
        ServerResponse serverResponse = costService.findCostByPatientId(patientId);
        return serverResponse;
    }

    //添加其他费用的住院天数和对应的金额
    @PutMapping("insertCostOther")
    public ServerResponse insertCostOther(@RequestHeader("token") String token,Integer patientId){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = patientService.insertCostOther(patientId,userOv.getId());
        costService.updataTotal(patientId);
        return serverResponse;
    }

    //查询总的费用(分页多表)
    @PostMapping("getCostList")
    @RequireRoles(roles="casher")
    public ServerResponse getCostList(@RequestHeader("token")String token,@RequestBody PageParam<CostOv> pageParam){
        return costService.getCostList(pageParam);
    }

    //查询药品的费用(分页多表)
    @PostMapping("getCostDrugDetailList")
    @RequireRoles(roles="casher,nurse,main_nurse")
    public ServerResponse getCostDrugDetailList(@RequestHeader("token")String token,@RequestBody PageParam<DruglistOv> pageParam){
        return costService.getCostDrugDetailList(pageParam);
    }

    //查询其他费用(分页多表)
    @PostMapping("getCostOtherList")
    @RequireRoles(roles="casher,nurse,main_nurse")
    public ServerResponse getCostOtherList(@RequestHeader("token")String token,@RequestBody PageParam<CostOtherOv> pageParam){
        return costService.getCostOtherList(pageParam);
    }

    /**
     * 获取的病人详情
     *
     * @return
     */
    @PostMapping("getPatientList")
    @RequireRoles(roles="casher,main_nurse")
    public ServerResponse getPatientList(@RequestHeader(value = "token") String token, @RequestBody PageParam<PatientOv> pageParam) {
        PageInfo pageInfo = patientService.getPatientList(pageParam);
        return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
    }

    //修改病人详情
    @PostMapping("updatePatientDetail")
    @RequireRoles(roles="casher")
    public ServerResponse updatePatientDetail(@RequestHeader(value = "token") String token, @RequestBody @Validated PatientOv patientOv) {
        ServerResponse serverResponse = userService.updatePatient(patientOv);
        return serverResponse;
    }

    //删除病人详情
    @PostMapping("deletePatientDetail")
    @RequireRoles(roles="casher")
    public ServerResponse deletePatientDetail(@RequestHeader(value = "token") String token, @RequestBody PatientDetail patientDetail) {
        ServerResponse serverResponse = patientService.deletePatientDetail(patientDetail);
        return serverResponse;
    }

    //医保支付
    @PostMapping("updateCostStatus1")
    @RequireRoles(roles="casher")
    public ServerResponse updateCostStatus1(@RequestHeader(value = "token") String token, @RequestBody CostOv costOv) {

        return costService.updateCostStatus1(costOv);
    }

    //其他支付
    @PostMapping("updateCostStatus2")
    @RequireRoles(roles="casher")
    public ServerResponse updateCostStatus2(@RequestHeader(value = "token") String token, @RequestBody CostOv costOv) {
        //System.out.println(costOv);
        return costService.updateCostStatus2(costOv);
    }
}
